home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15511 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  157 lines

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.programming,comp.lang.c++
  4. Subject: Re: C++ problem with objects and pointers (I think).
  5. Date: Fri, 05 Apr 1996 18:05:34 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <3165A73E.F98@datalytics.com>
  8. References: <3161F52E.3561@abc.se>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Kjell Franzen wrote:
  16. > Using a debugger shows me that putting the values there works fine, but
  17. > when i want to get them and print to screen I get totally out of the
  18. > blue (out of the Ram:)) values. Do my program "forget" the adresses, is
  19. > my pointer initiation wrong? HELP!
  20.  
  21. First, let me point out that C++ questions like this should be 
  22. directed to comp.lang.c++ or c.l.c++.moderated in the future.  
  23. However, you'll be looking for the answer in comp.programming, 
  24. so I'll reply there.
  25.  
  26. ---------------------------------------------------------------
  27. [snip]
  28. > class Object
  29. > {
  30. >    private:
  31. >      point *vertices;
  32. >      facet *polygons;
  33. >      int n_vertices;
  34. >      int n_polygons;
  35. >    public:
  36. >      Object(void);
  37. >      void init_object(void);
  38. >      void print_object(void);
  39. > };
  40. > Object::Object(void)
  41. > {
  42. >     //here I zero out all variables, structs and pointers
  43. > };
  44.  
  45.    ^ not needed here
  46.  
  47. You should implement the ctor with an initializer list; it's a 
  48. good habit to develop:
  49.  
  50. Object::Object(void):
  51.     vertices(0),
  52.     polygons(0),
  53.     n_vertices(0),
  54.     n_polygons(0)
  55. {
  56. }
  57.  
  58. [snip]
  59. > void Object::init_object(void)
  60. > {
  61. >     n_vertices = 3;
  62. >     n_polygons = 1;
  63. >     struct point *vertices = new struct point[n_vertices];
  64. >     struct facet *polygons = new struct facet[n_polygons];
  65.  
  66. There is a problem with the lines above.  You have declared two 
  67. new pointers here.  These have scope throughout the function 
  68. from the point of declaration forward.  (Also, the keyword 
  69. struct is not strictly necessary in C++.)  Try this:
  70.  
  71.     vertices = new point[n_vertices];
  72.     polygons = new facet[n_polygons];
  73.  
  74. [snip]
  75. > void Object::print_object(void)
  76. > {
  77. >     cout << "n_vertices: " << n_vertices << endl;
  78. >     cout << "n_polygons: " << n_polygons << endl;
  79. >     for (int i = 0 ; i < n_vertices ; i++)
  80. >     {
  81. >         cout << "vertices[" << i << "].x: " << vertices[i].x << endl;
  82. >         cout << "vertices[" << i << "].y: " << vertices[i].y << endl;
  83. >         cout << "vertices[" << i << "].z: " << vertices[i].z << endl;
  84. >     };
  85.  
  86.        ^ not needed here
  87.  
  88. >     for (i = 0 ; i < n_polygons ; i++)
  89. >      for (int j = 0 ; j < 3 ; j++)
  90. >       cout<<"polygons["<< i <<"].vtx["<< j <<"]: "<<polygons[i].vtx[j]<<"\n";
  91. > }
  92.  
  93. You can combine these lines and improve output performance like 
  94. this:
  95.  
  96. inline    // or not; your call
  97. void DisplayVertex(
  98.    const int i_Index,
  99.    const char i_Vertex,
  100.    const long i_Value)
  101. {
  102.    cout << "\nvertices[" << i_Index << "]." << i_Vertex << ": "
  103.       i_Value;
  104. }
  105.  
  106. void Object::print_object(void)
  107. {
  108.    cout << "n_vertices: " << n_vertices
  109.       << "\nn_polygons: " << n_polygons;
  110.    for (int i = 0; i < n_vertices; i++)
  111.    {
  112.       DisplayVertex(
  113.          i,
  114.          'x',
  115.          vertices[i].x);
  116.       DisplayVertex(
  117.          i,
  118.          'y',
  119.          vertices[i].y);
  120.       DisplayVertex(
  121.          i,
  122.          'z',
  123.          vertices[i].z);
  124.    }
  125.    for (i = 0; i < n_polygons; i++)
  126.    {
  127.       for (int j = 0; j < 3; j++)
  128.       {
  129.          cout << "\npolygons[" << i << "].vtx[" << j << "]: "
  130.             << polygons[i].vtx[j];
  131.       }
  132.    }
  133.    cout << endl;
  134. }
  135.  
  136. The differences are several.  First, the inline function avoids 
  137. code duplication without changing the end result.  You can 
  138. change the inline function once and affect all three uses of it.  
  139. (Whether to make inline or not is a space-time tradeoff you'll 
  140. have to make.)
  141.  
  142. Second, note that I am using newlines rather than calling the 
  143. endl manipulator in all but the last line.  The difference can 
  144. be pronounced.  You see, endl not only inserts a newline, it 
  145. also calls the flush mf.  Thus, your implementation wasn't 
  146. allowing much use of the output buffer in cout.
  147.  
  148. Finally, note that you don't have to reference cout as many 
  149. times as you did.  There is no penalty in doing so, aside from 
  150. the lost use of a temporary each time.  Besides, an optimizing 
  151. compiler will probably not generate those unused temporaries.
  152.  
  153. -- 
  154. Robert Stewart        | My opinions are usually my own.
  155. Datalytics, Inc.    | stew@datalytics.com
  156.